package com.eight.controller;

import com.eight.util.SMSUtil;
import com.eight.util.WebUtil;
import com.eight.vo.ComResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * Created by Intellij IDEA<br>
 * 工具Controller，用于处理一些非业务请求
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/10/13 20:46
 */
@RestController
@RequestMapping("/Util")
public class UtilController {
    @Autowired
    private StringRedisTemplate redis;


    /**
     * 用来获取短信验证码的Controller
     *
     * @return 公共的结果集
     */
    @RequestMapping("/SMSVer/{uid}")
    @ResponseBody
    public ComResult smsVerify(@PathVariable String uid) {
        String code = SMSUtil.smsVerification();
        if (!code.isEmpty()) {
            redis.opsForValue().set("SMSCode"+uid,code,5, TimeUnit.MINUTES);
            return new ComResult(2000, "获取成功",code);
        } else {
            return new ComResult(5000, "获取失败",code);
        }
    }

    /**
     * 校验验证码是否正确
     * @param uid 用户id
     * @param code 验证码
     * @return 校验结果
     */
    @RequestMapping("/VfCode/{code}/{uid}")
    @ResponseBody
    public ComResult vfCode(@PathVariable String uid, @PathVariable String code) {
       if (code.equals(redis.opsForValue().get("SMCode"+uid))){
           return new ComResult(2000,"校验通过！");
       }else {
           return new ComResult(5000,"校验失败！");
       }
    }



}

